// src/stores/useUserStore.ts
import { defineStore } from 'pinia'
export const useUserStore = defineStore('user', {
  state: () => ({
    isLoggedIn: false,
    userId: null as string | null,
    userName: null as string | null,
    userEmail: null as string | null,
  }),
  actions: {
    login(userId: string, userName: string, userEmail: string) {
      this.isLoggedIn = true
      this.userId = userId
      this.userName = userName
      this.userEmail = userEmail
      localStorage.setItem('isLoggedIn', 'true')
      localStorage.setItem('userId', userId)
      localStorage.setItem('userName', userName)
      localStorage.setItem('userEmail', userEmail)
    },
    logout() {
      this.isLoggedIn = false
      this.userId = ''
      this.userName = ''
      this.userEmail = ''
      localStorage.removeItem('isLoggedIn')
      localStorage.removeItem('userId')
      localStorage.removeItem('userName')
      localStorage.removeItem('userEmail')
    },
    restoreFromLocalStorage() {
      const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
      const userId = localStorage.getItem('userId') || ''
      const userName = localStorage.getItem('userName') || ''
      const userEmail = localStorage.getItem('userEmail') || ''
      this.isLoggedIn = isLoggedIn
      this.userId = userId
      this.userName = userName
      this.userEmail = userEmail
    }
  }
})